using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FernUniHagen.CompilerExtension.DataStructures;
using FernUniHagen.CompilerExtension.Functions;
using FernUniHagen.CompilerExtension.Interfaces;
using Roslyn.Compilers.Common;
using Roslyn.Compilers.CSharp;

namespace FernUniHagen.CompilerExtension.Adaptor
{
    public class MultExpressionAdapter : BlockNodeFactory, IMultiplicityConvertible<CommonSyntaxNode>
    {
        BinaryExpressionSyntax _binaryExpresion;

        public MultExpressionAdapter(BinaryExpressionSyntax node)
        {
            _binaryExpresion = node;
        }
        public override StatementReplacement<CommonSyntaxNode> ConvertToMutiplicity(string newVariableName)
        {
            var statementReplacement = new StatementReplacement<CommonSyntaxNode>();
            //this Binary Expression contains ^variable^ (the full statement. Use this for the replacement)
            var parentBinaryExpression = _binaryExpresion.Parent as BinaryExpressionSyntax;

            //take the right side of the statement ^rightSide
            var declaration = SemanticModel.FindDecleration(_binaryExpresion.Right as IdentifierNameSyntax);
            var decl = SyntaxElementFactory.GetDeclaration(declaration, SemanticModel);


            switch (decl.MultiplicityType)
            {
                case AttributeType.Any:
                    var rmult2Expression = Syntax.ParseExpression(string.Format("{0}.Count", _binaryExpresion.Right.ToString()));
                    statementReplacement = new StatementReplacement<CommonSyntaxNode>(parentBinaryExpression, rmult2Expression);
                    break;
                case AttributeType.Option:
                    var rmult1Expression = Syntax.ParseExpression(string.Format("{0} == null ? 0 : 1", _binaryExpresion.Right.ToString()));
                    statementReplacement = new StatementReplacement<CommonSyntaxNode>(parentBinaryExpression, rmult1Expression);
                    break;
                default:
                    statementReplacement = new StatementReplacement<CommonSyntaxNode>(_binaryExpresion, _binaryExpresion);
                    break;
            }

            return statementReplacement;
        }
       
    }
}
